package com.ljfresh.customer.erp;

import com.alibaba.fastjson.JSONObject;
import com.ljfresh.customer.dto.ErpGoodDto;
import com.ljfresh.customer.dto.ErpStoreDto;
import com.ljfresh.customer.dto.ErpTransferinDto;
import com.ljfresh.customer.pojo.Store;
import com.ljfresh.customer.result.FindTransferinResult;
import com.ljfresh.customer.result.GoodERPResult;
import com.ljfresh.customer.result.StoreERPResult;
import com.ljfresh.customer.utils.HttpClientUtil;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: Lucifer
 * @time: 2020/4/13-10:50
 * ERP接口请求-基础资料
 */
public class ERPBasicAPIUtil {

    /**
     * 查询所有门店
     * @return
     */
    public static List<StoreERPResult> findStore(String token){

        List<Store> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("isopen",true);
        map.put("page_no",1);
        map.put("page_size",1000);
        String paramStr = ERPParamBuilder.paramStr(map);
        try {
            String resultStr = HttpClientUtil.getApiJson(ERPConsts.basicUrl+ERPConsts.get_store_url+"?"+paramStr,token);
            if (StringUtils.isEmpty(resultStr)) {
                return new ArrayList<>();
            }
            System.out.println(resultStr);
            ErpStoreDto erpStoreDto = JSONObject.parseObject(resultStr, ErpStoreDto.class);
            return erpStoreDto.getResult();
        }catch (Exception e){
            return null;
        }
    }

    public static List<GoodERPResult> findGoods(String token,Integer pageNo){
        List<Store> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("page_no",pageNo);
        map.put("page_size",500);
        String paramStr = ERPParamBuilder.paramStr(map);
        try {
            String resultStr = HttpClientUtil.getApiJson(ERPConsts.basicUrl+ERPConsts.get_good_url+"?"
                    +paramStr,token);
            if (StringUtils.isEmpty(resultStr)) {
                return new ArrayList<>();
            }
            System.out.println(resultStr);
            ErpGoodDto erpGoodsDto = JSONObject.parseObject(resultStr, ErpGoodDto.class);

            return erpGoodsDto.getResult();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    public static List<FindTransferinResult> transferin(String token, String storeCode,Date date){
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Map<String,Object> map = new HashMap<>();
            map.put("branch_num",storeCode);
            map.put("date_to",simpleDateFormat.format(date));
            /*Calendar instance = Calendar.getInstance();
            instance.setTime(date);
            instance.add(Calendar.DATE,-1);
            map.put("date_from",simpleDateFormat.format(instance.getTime()));*/
            map.put("date_from",simpleDateFormat.format(date));
            map.put("page_no",1);
            map.put("page_size",10);
            map.put("query_detail",true);
            map.put("order_state",2);
            map.put("date_type","审核时间");
            String paramStr = ERPParamBuilder.paramStr(map);
            String resultStr = HttpClientUtil.getApiJson(ERPConsts.basicUrl+ERPConsts.transfer_in_url+"?"+paramStr,token);
            if (StringUtils.isEmpty(resultStr)) {
                return null;
            }
            ErpTransferinDto erpTransferinDto = JSONObject.parseObject(resultStr, ErpTransferinDto.class);
            for (FindTransferinResult transferinResult:
            erpTransferinDto.getResult()) {
                System.out.println(transferinResult.getOut_order_fid());
            }
            System.out.println(resultStr);
            return erpTransferinDto.getResult();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
